/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年8月7日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.sqlite.core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.apollo.sqlite.utils.DBConfig;
import org.apollo.sys.SysLog;
import org.sqlite.javax.SQLiteConnectionPoolDataSource;

/**
 * @ClassName: SQLiteDatabase
 * @Description: 数据源
 * @author wuzhenfang(wzfbj2008@163.com)
 * @date 2017年8月7日 下午4:35:02
 * @version V1.0
 */
public class SQLiteDatabase extends SQLiteConnectionPoolDataSource {

	private final static Logger Log = SysLog.getLogger(SQLiteDatabase.class);

	public DBConfig config;

	public SQLiteDatabase(DBConfig config) {
		super(config);
		this.config = config;
		init();
	}

	private void init() {
		if (this.config.getDatabaseURL() != null) {
			setUrl(this.config.getDatabaseURL());
		}
		if (this.config.getDatabaseName() != null) {
			setDatabaseName(this.config.getDatabaseName());
		}
		if (this.config.getDatabaseVersion() != 0) {
			setUserVersion(this.config.getDatabaseVersion());
		}
		if (this.config.getEncoding() != null) {
			setEncoding(this.config.getEncoding());
		}
	}

	@Override
	public void setUrl(String url) {
		super.setUrl(url);
	}

	@Override
	public void setDatabaseName(String databaseName) {
		super.setDatabaseName(databaseName);
	}

	@Override
	public void setEncoding(String encoding) {
		super.setEncoding(encoding);
	}

	@Override
	public void setUserVersion(int version) {
		super.setUserVersion(version);
	}

	public static void main(String[] args) {
		Connection connection = null;
		try {
			// create a database connection
			connection = DriverManager.getConnection("jdbc:sqlite:sample.db");// 默认是resources文件夹下
			Statement statement = connection.createStatement();
			statement.setQueryTimeout(30); // set timeout to 30 sec.

			statement.executeUpdate("drop table if exists person");
			statement.executeUpdate("create table person (id integer, name string)");
			statement.executeUpdate("insert into person values(1, 'leo')");
			statement.executeUpdate("insert into person values(2, 'yui')");
			ResultSet rs = statement.executeQuery("select * from person");
			while (rs.next()) {
				// read the result set
				System.out.println("name = " + rs.getString("name"));
				System.out.println("id = " + rs.getInt("id"));
			}
		} catch (SQLException e) {
			// if the error message is "out of memory",
			// it probably means no database file is found
			System.err.println(e.getMessage());
		} finally {
			try {
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				// connection close failed.
				System.err.println(e);
			}
		}
	}

}
